package com.woniuxy.usercoachassemble.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.woniuxy.usercoachassemble.entity.Account;
import com.woniuxy.usercoachassemble.entity.CoachInfo;
import com.woniuxy.usercoachassemble.entity.YogaGymInfo;
import com.woniuxy.usercoachassemble.entity.YogaGymSignedCoach;
import com.woniuxy.usercoachassemble.service.CoachInfoService;
import com.woniuxy.usercoachassemble.service.UserinfoService;
import com.woniuxy.usercoachassemble.service.YogaGymInfoService;
import com.woniuxy.usercoachassemble.service.YogaGymSignedCoachService;
import org.example.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-07-09
 */
@RestController
@RequestMapping("/usercoachassemble/coach-info")
public class CoachInfoController {
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private CoachInfoService coachInfoService;
    @Autowired
    private YogaGymInfoService yogaGymInfoService;
    @Autowired
    private YogaGymSignedCoachService yogaGymSignedCoachService;

    /**
     * 查看我被招聘的瑜伽馆
     * @param request
     * @return
     */
    @GetMapping("selectChooseMe")
    public Result selectChooseMe(HttpServletRequest request){
        int userId = Integer.parseInt(request.getHeader("userId"));
        List<Map<String, Object>> maps = userinfoService.selectChooseMe(userId);

        return Result.Success(maps);
    }

    /**
     * 教练选着瑜伽管
     * @param request
     * @param GymId
     * @return
     */
    @GetMapping("selectChooseGym")
    public Result selectChooseGym(HttpServletRequest request, int GymId) {
        //获得教练ID
        int userId = Integer.parseInt(request.getHeader("userId"));
        //删除其他招聘信息,更改选中瑜伽管状态为2(已签约)

        UpdateWrapper<YogaGymSignedCoach> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("yoga_gym_signed_coach_id",GymId);
        queryWrapper.eq("coach_id",userId);
        YogaGymSignedCoach yogaGymSignedCoach = new YogaGymSignedCoach();
        yogaGymSignedCoach.setStatus(2);
        yogaGymSignedCoachService.update(yogaGymSignedCoach,queryWrapper);
        //更改个人信息改为已签约
        QueryWrapper<CoachInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        CoachInfo one = coachInfoService.getOne(wrapper);
        one.setB("3");
        coachInfoService.saveOrUpdate(one);
        //删除其他瑜伽馆的消息
        UpdateWrapper<YogaGymSignedCoach> queryWrapper1 = new UpdateWrapper<>();
        queryWrapper1.eq("status","1");
        queryWrapper1.eq("coach_id",userId);
        boolean remove = yogaGymSignedCoachService.remove(queryWrapper1);
        if(remove){
            return Result.Success();
        }
        return Result.Error();
    }

    /**
     * 查询账户
     * @param request

     * @return
     */
    @GetMapping("selectAccount")
    public Result selectAccount(HttpServletRequest request) {
        int userId= Integer.parseInt(request.getHeader("userId"));
        Account account = coachInfoService.selectAccount(userId);

        return Result.Success(account);

    }

    /**
     * 创建账户
     * @param request
     * @param accountName
     * @return
     */
    @PostMapping("setMoney")
    public Result selectAccountMoney(HttpServletRequest request, String accountName){
        int userId= Integer.parseInt(request.getHeader("userId"));
        coachInfoService.insertAccountMoney( userId,  accountName);
        return Result.Success();
    }

    /**
     * 查看自己签署公司
     * @param request
     * @return
     */
    @GetMapping("getGymByMe")
    public Result authCoachByGym(HttpServletRequest request) {
        int userId= Integer.parseInt(request.getHeader("userId"));
        QueryWrapper<YogaGymSignedCoach> wrapper = new QueryWrapper<>();
        wrapper.eq("coach_id",userId);
        wrapper.eq("status",2);
        YogaGymSignedCoach one = yogaGymSignedCoachService.getOne(wrapper);
        Integer yogaGymId = one.getYogaGymId();
        QueryWrapper<YogaGymInfo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id",yogaGymId);
        YogaGymInfo one1 = yogaGymInfoService.getOne(wrapper1);


        return Result.Success(one1);
    }

    /**
     * 认证
     * @param CoachId
     * @return
     */
    @GetMapping("authCoachByGym")
    public Result authCoachByGym( int CoachId) {

        QueryWrapper<CoachInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",CoachId);
        CoachInfo one = coachInfoService.getOne(wrapper);
        if(one.getIndentification()==2){
            return Result.Error(600,"会馆已验证,无需再次验证");
        }else if(one.getIndentification()==3){
            return Result.Error(600,"平台已验证,无需再次验证");
        }
        one.setIndentification(2);
        coachInfoService.updateById(one);
        return Result.Success();
    }


}

